$(function () {
    //表格初始加载
    $('#grid_kcpd').datagrid({
        url:$.webPath() + "/kcgl/kcpd/kcpdList",
        toolbar:"#toolbar_kcpd",
        fitColumns:true,
        singleSelect:true,
        pagination:true,
        pageNumber:1,
        fit:true,
        rownumbers:true,
        striped:true,
        columns:[
            [
                {field:'kcpdbm', title:'盘点单号', align:'center', width:60, formatter:pdmxCell},
                {field:'pdck_mc', title:'盘点仓库', align:'center', width:80},
                {field:"pdyf", title:"盘点月份", align:'center', width:60},
                {field:"lastUpdated_time", title:"盘点日期", align:'center', width:70},
                {field:'zmje', title:'账面金额(元)', align:'right', width:60,
                    formatter:function (value) {
                        var val = formatJeForHtml(value);
                        return val;
                    }},
                {field:'spje', title:'实盘金额(元)', align:'right', width:60,
                    formatter:function (value) {
                        var val = formatJeForHtml(value);
                        return val;
                    }},
                {field:'yksl', title:'盈亏数量', align:'right', width:50,
                    formatter:function (value) {
                        var val = formatSlForHtml(value);
                        return val;
                    }},
                {field:'ykje', title:'盈亏金额(元)', align:'right', width:50,
                    formatter:function (value) {
                        var val = formatJeForHtml(value);
                        return val;
                    }},
                {field:'pdr_xm', title:'盘点人', align:'center', width:30},
                {field:'shry_xm', title:'审核人', align:'center', width:30},
                {field:'shrq', title:'审核日期', align:'center', width:60}
            ]
        ],
        onLoadSuccess:function () {
            $(".datagrid-htable .datagrid-cell").css('text-align', 'center');
        },
        onSelect:function () {
            kcpd_setBtnStatus();
        }
    });

    //**********按钮触发事件**********
    //新增按钮
    $("#btn_kcpd_add").click(function () {
        kcpd_add('新增库存盘点', 'add_kcpd');
    });
    //删除按钮
    $("#btn_kcpd_del").click(function () {
        kcpd_delete();
    });
    //审核按钮
    $("#btn_kcpd_sh").click(function () {
        kcpd_sh();
    });
    //实盘按钮
    $("#btn_kcpd_sp").click(function () {
        kcpd_spxx('实盘信息', 'sava_pdmx');
    });
    //查询按钮
    $("#btn_kcpd_search").click(function () {
        kcpd_search();
    });
    //双击事件
    $("#grid_kcpd").datagrid({
        onDblClickCell:function (index, field) {
            if (field == "kcpdbm") {
                kcpd_modify('修改库存盘点', 'update_kcpd');
            }
        }
    });
    //库存盘点导出
    $("#kcpd_Export").click(function () {
        var record = $("#grid_kcpd").datagrid("getSelected");
        if (record) {
            window.open($.webPath() + "/kcgl/kcpd/kcpd_export?kcpdId=" + record.id);
        } else {
            $.messager.alert("提示", "请先选择你要导出的盘点单！", "warning");
        }
    });
    //加载仓库列表
    $('#kcpd_ck').combobox({
        url:$.webPath() + '/organ/ck/ckmcList',
        valueField:'id',
        textField:'mc',
        panelHeight:'200',
        editable:true
    });
});

//按钮状态
function kcpd_setBtnStatus() {
    if ($('#grid_kcpd').datagrid('getSelected')) {
        if ($('#grid_kcpd').datagrid('getSelected').sfsh == "是") {
            $("#btn_kcpd_del").hide();
            $("#btn_kcpd_sp").hide();
            $("#btn_kcpd_sh").hide();
        } else {
            $("#btn_kcpd_del").show();
            $("#btn_kcpd_sp").show();
            $("#btn_kcpd_sh").show();
        }
    } else {
        $("#btn_kcpd_del").hide();
        $("#btn_kcpd_sp").hide();
        $("#btn_kcpd_sh").hide();
    }
}

//新增库存盘点
function kcpd_add(title, action) {
    var href = $.webPath() + "/kcgl/kcpd/kcpd_content";
    var actionUrl = $.webPath() + "/kcgl/kcpd/" + action;
    kcpd_showDialog(title, href, actionUrl);
}

//修改库存盘点
function kcpd_modify(title, action) {
    var record = $("#grid_kcpd").datagrid("getSelected");
    if (record == null) {
        $.messager.alert("提示", "请选择要更新的记录！", "warning");
        return;
    }
    var href = $.webPath() + "/kcgl/kcpd/kcpd_content?kcpdId=" + record.id;
    var actionUrl = $.webPath() + "/kcgl/kcpd/" + action;
    kcpd_showDialog(title, href, actionUrl);
}

//删除库存盘点
function kcpd_delete() {
    var record = $('#grid_kcpd').datagrid('getSelected');
    if (record) {
        $.messager.confirm('提示', '是否删除该库存盘点？', function (r) {
            if (r) {
                var index = $('#grid_kcpd').datagrid('getRowIndex', record);
                $.ajax({
                    url:$.webPath() + "/kcgl/kcpd/delete_kcpd",
                    dataType:'json',
                    type:'POST',
                    data:{kcpdId:record.id},
                    beforeSend:function () {
                        $("#layout_kcpd").mask();
                    },
                    success:function (data) {
                        $("#layout_kcpd").mask("hide");
                        kcpd_setBtnStatus();
                        if (data.statusCode == 200) {
                            $('#grid_kcpd').datagrid('deleteRow', index);
                            $.messager.show({
                                title:'提示',
                                msg:data.message,
                                timeout:3000,
                                showType:'slide'
                            });
                        } else {
                            $.messager.alert("提示", data.message, "error");
                        }
                    }
                });
            }
        });
    } else {
        $.messager.alert("提示", "请选择要删除的库存盘点！", "warning");
    }
}

//删除库存盘点
function kcpd_sh() {
    var record = $('#grid_kcpd').datagrid('getSelected');
    if (record) {
        $.messager.confirm('提示', '是否审核该库存盘点？', function (r) {
            if (r) {
                var index = $('#grid_kcpd').datagrid('getRowIndex', record);
                $.ajax({
                    url:$.webPath() + "/kcgl/kcpd/sh_kcpd",
                    dataType:'json',
                    type:'POST',
                    data:{kcpdId:record.id},
                    beforeSend:function () {
                        $("#layout_kcpd").mask();
                    },
                    success:function (data) {
                        $("#layout_kcpd").mask("hide");
                        kcpd_setBtnStatus();
                        if (data.statusCode == 200) {
                            $('#grid_kcpd').datagrid('deleteRow', index);
                            $.messager.show({
                                title:'提示',
                                msg:data.message,
                                timeout:3000,
                                showType:'slide'
                            });
                        } else {
                            $.messager.alert("提示", data.message, "error");
                        }
                    }
                });
            }
        });
    } else {
        $.messager.alert("提示", "请选择要删除的库存盘点！", "warning");
    }
}

//新增或修改库存盘点弹出窗口
function kcpd_showDialog(title, href, actionUrl) {
    var win = $.window({
        title:title,
        width:620,
        height:300,
        cache:false,
        url:href,
        modal:true,
        buttons:[
            {
                text:'保存',
                handler:function () {
                    $(win).mask();
                    $('#form_kcpd').form('submit', {
                        url:actionUrl,
                        onSubmit:function () {
                            var isValid = $(this).form('validate');
                            if (!isValid) {
                                $(win).mask('hide');
                                return false;
                            }
                            return isValid;
                        },
                        success:function (data) {
                            $(win).mask('hide');
                            var json = $.parseJSON(data);
                            if (json.statusCode == 200) {
                                $(win).window('close');
                                $('#grid_kcpd').datagrid('reload');
                                $.messager.show({
                                    title:'提示',
                                    msg:json.message,
                                    timeout:3000,
                                    showType:'slide'
                                });
                            } else
                                $.messager.alert('提示', json.message, 'error');
                        }
                    });
                }
            }
        ]
    });
}

//库存盘点查询
function kcpd_search() {
    var ckId = $('#kcpd_ck').combotree('getValue') == null ? null : $('#kcpd_ck').combotree('getValue');
    $('#grid_kcpd').datagrid('load',
        {
            ckId:ckId,
            ckMc:$('#kcpd_ck').val()
        }
    );
}

//实盘明细
function kcpd_spxx(title, action) {
    var record = $("#grid_kcpd").datagrid("getSelected");
    if (record == null) {
        $.messager.alert("提示", "请选择要查看的记录！", "warning");
        return;
    }
    var href = $.webPath() + "/kcgl/kcpd/kcpd_detail?kcpdId=" + record.id;
    var actionUrl = $.webPath() + "/kcgl/kcpd/" + action;
    kcpd_showSpxxDialog(title, href, actionUrl);
}

function kcpd_showSpxxDialog(title, href, actionUrl) {
    var win = $.window({
        title:title,
        width:1000,
        height:480,
        cache:false,
        url:href,
        modal:true,
        buttons:[
            {
                text:'保存',
                handler:function () {
                    $(win).mask();
                    $("#kcpd_detail").edatagrid("saveRow");
                    $("#pdmx_form").form('submit', {
                        url:actionUrl,
                        onSubmit:function (param) {
                            var isValid = $(this).form('validate');
                            if (!isValid) {
                                $(win).mask('hide');
                                return false;
                            }
                            var data = $("#kcpd_detail").edatagrid("getData");
                            var arrayPrefix = "kcpd.pdmxList";
                            var paramString = "{";
                            for (var i = 0; i < data.rows.length; i++) {
                                var objectString = $.toJSONStringSubPrefix(data.rows[i], arrayPrefix + "[" + i + "].", ['id', 'spsl', 'yksl', 'ykje']);
                                paramString = paramString + objectString.substring(1, objectString.length - 1) + ",";
                            }
                            if (data.rows && data.rows.length > 0)
                                paramString = paramString.substring(0, paramString.length - 1);
                            paramString += "}";
                            $.extend(param, $.parseJSON(paramString));
                            return isValid;
                        },
                        success:function (data) {
                            $(win).mask('hide');
                            var json = $.parseJSON(data);
                            if (json.statusCode == 200) {
                                $(win).window('close');
                                $('#grid_kcpd').datagrid('reload');
                                $.messager.show({
                                    title:'提示',
                                    msg:json.message,
                                    timeout:3000,
                                    showType:'slide'
                                });
                            } else
                                $.messager.alert('提示', json.message, 'error');
                        }
                    });
                }
            }
        ]
    });
}